package com.hutao.manage.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.manage.datasource.TargetDataSource;
import com.hutao.manage.mapper.TreatmentTraceMapper;
import com.hutao.manage.mapper.TreatmentTypeMapper;
import com.hutao.manage.pojo.TreatmentTrace;
import com.hutao.manage.query.MedTraceQuery;
import com.hutao.manage.service.TreatmentTraceService;
@Service
public class TreatmentTraceServiceImpl implements TreatmentTraceService {

	@Autowired
	private TreatmentTraceMapper treatmentTraceMapper;
	
	@Autowired
	private TreatmentTypeMapper treatmentTypeMapper;
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> selectTraceByMedTraceQuery(
			MedTraceQuery medTraceQuery) {
		// TODO Auto-generated method stub
		return treatmentTraceMapper.selectTraceByMedTraceQuery(medTraceQuery);
	}

	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public boolean addMedTraceQuery(TreatmentTrace treatmentTrace) {
		boolean result = false;
		int count =treatmentTraceMapper.insertSelective(treatmentTrace);
		if(count>0){
			result= true;
		}
		return result;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> selectMedShelveByTraceType(
			MedTraceQuery medTraceQuery) {
		
		 List<Map<String,Object>> mapList = treatmentTraceMapper.selectMedShelveByTraceType(medTraceQuery);
		 for (Map<String, Object> map : mapList) {
			String guid =  (String)map.get("treatment_guid");
			
			List<Map<String,Object>> treatmentTyps = treatmentTraceMapper.getTreatmentTraceTypeByCode(TreatmentTrace.VISIT_PAGINATION);
			int flagCount = treatmentTraceMapper.isPagenationTreatementTrace(guid, (Integer)treatmentTyps.get(0).get("id"));
			map.put("isPage", flagCount>0?2:1);
		 }
		
		
		return mapList;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public int selectMedShelveByTraceTypeCount(MedTraceQuery medTraceQuery) {
	
		return treatmentTraceMapper.selectMedShelveByTraceTypeCount(medTraceQuery);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public int selectTraceByMedTraceQueryCount(MedTraceQuery medTraceQuery) {
		return treatmentTraceMapper.selectTraceByMedTraceQueryCount(medTraceQuery);
	}

	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public boolean addTreatmentTraceHomePage(String visitGuid,String currentUser) {
		// TODO Auto-generated method stub
		TreatmentTrace treatmentTrace = new TreatmentTrace();
		treatmentTrace.setUserId(currentUser);
		List<Map<String,Object>> treatmentTyps = treatmentTraceMapper.getTreatmentTraceTypeByCode(TreatmentTrace.VISIT_PAGE);
		treatmentTrace.setTraceTypeId((Integer)treatmentTyps.get(0).get("id"));
		treatmentTrace.setTreatmentGuid(visitGuid);
		int count = treatmentTraceMapper.insert(treatmentTrace);
		if(count > 0){
			return true;
		}else{
			return false;
		}
		
	}

	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public boolean addMedicalTrace(TreatmentTrace treatmentTrace,
			String treatmentTraceTypeCode) {
		// TODO Auto-generated method stub
		boolean result = false;
		List<Map<String,Object>> treatmentTyps = treatmentTraceMapper.getTreatmentTraceTypeByCode(treatmentTraceTypeCode);
		treatmentTrace.setTraceTypeId((Integer)treatmentTyps.get(0).get("id"));
		int flagCount = treatmentTraceMapper.isPagenationTreatementTrace(treatmentTrace.getTreatmentGuid(), (Integer)treatmentTyps.get(0).get("id"));
		if(flagCount>0){
			return true;
		}
		int count =treatmentTraceMapper.insertSelective(treatmentTrace);
		if(count>0){
			result= true;
		}
		return result;
	}

	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int updateMedTraceQuery(TreatmentTrace treatmentTrace) {
		return treatmentTraceMapper.updateByPrimaryKeySelective(treatmentTrace);
	}

	/**
	 * 
	 * 归档工作量统计 
	 * 
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly = true)
	@Override
	public List<Map<String, Object>> getWorkloadStatisticsDateOfFiling(Map<String, Object> params) {
		return treatmentTraceMapper.getWorkloadStatisticsDateOfFiling(params);
	}

	/**
	 * 质控工作量
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly = true)
	@Override
	public List<Map<String, Object>> getWorkloadStatisticsQualityControl(Map<String, Object> params) {
		return treatmentTraceMapper.getWorkloadStatisticsQualityControl(params);
	}

	/**
	 * 编页工作量统计
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly = true)
	@Override
	public List<Map<String, Object>> getWorkloadStatisticsPagenation(Map<String, Object> params) {
		return treatmentTraceMapper.getWorkloadStatisticsPagenation(params);
	}

	/**
	 * 编目工作量统计
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly = true)
	@Override
	public List<Map<String, Object>> getWorkloadStatisticsCatalog(Map<String, Object> params) {
		return treatmentTraceMapper.getWorkloadStatisticsCatalog(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly = true)
	@Override
	public List<Integer> selectTraceByGuidAndCode(String firstGuid, String traceCode) {
		
		return treatmentTraceMapper.selectTraceByGuidAndCode(firstGuid, traceCode);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly = true)
	@Override
	public void deleteById(Integer id) {

		treatmentTraceMapper.deleteByPrimaryKey(id);
	}

	
	
	
	
	
	
	
	
	

}








